<?php if (!defined('THINK_PATH')) exit();?><!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<title>运营平台</title>
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="renderer" content="webkit">
<!--<meta http-equiv="X-UA-Compatible" content="IE=edge">-->
<link type="text/css" rel="stylesheet" href="<?php echo get_style_cdnurl();?>paimai/yunying/css/common.css?v=20170207" />
<link type="text/css" rel="stylesheet" href="<?php echo get_style_cdnurl();?>paimai/yunying/css/table.css?v=20170117" />
<link type="text/css" rel="stylesheet" href="<?php echo get_style_cdnurl();?>paimai/yunying/css/jquery-ui.css" />
<script src="<?php echo get_style_cdnurl();?>paimai/yunying/js/jquery-1.11.1.js" type="text/javascript"></script>
<script src="<?php echo get_style_cdnurl();?>paimai/yunying/js/jquery-ui.min.js" type="text/javascript"></script>
 <script src="<?php echo get_style_cdnurl();?>paimai/yunying/js/yunIndex.js" type="text/javascript"></script>
<script src="<?php echo get_style_cdnurl();?>paimai/yunying/js/jquery-ym-datePlugin-0.1.js" type="text/javascript"></script>
<script src="<?php echo get_style_cdnurl();?>paimai/yunying/js/global.js" type="text/javascript"></script>
<script src="<?php echo get_style_cdnurl();?>paimai/cheshan/js/common.js" type="text/javascript"></script>
<script type="text/javascript" src="<?php echo get_style_cdnurl();?>paimai/yunying/js/jquery.placeholder.min.js"></script>
<script type="text/javascript">
 $(function(){
     $("input").placeholder();
     if (navigator.userAgent.indexOf("Safari") > -1) {
       $("input").css("lineHeight","normal !important");
     };
});
</script>

<body>
<div class="header">
<div class="headerCo">
<span>你好,<?php echo ($user_info["real_name"]); ?><a href="<?php echo (WEB_URL); ?>Emp/logout">退出</a></span>
<h1><img src="<?php echo get_style_cdnurl();?>paimai/yunying/images/yun_logo.jpg"></h1>
</div>
</div>
<link type="text/css" rel="stylesheet" href="<?php echo get_style_cdnurl();?>paimai/yunying/css/cashier.css" />
<div class="mian">
    <div class="mianL">
    <dl>
        <dd>
            <a id="menu_sy" href="/Index/index" class="shouye">首页</a>
        </dd>
        <?php foreach ($tree_menu as $key => $value): ?>
          <dd>
            <span id="menu_<?php echo ($value['first_char']); ?>" ><img src="<?php echo get_style_cdnurl();?>paimai/yunying/images/yun_jia.jpg"><?php echo ($value['name']); ?></span>
            <ul style="display: none;">
                <?php if (!empty($value['children'])): ?>
                    <?php foreach ($value['children'] as $k => $v): ?>
                        <li><a id='menu_<?php echo ($v["first_char"]); ?>' href="<?php echo ($v['url']); ?>"><?php echo ($v['name']); ?></a></li>
                    <?php endforeach ?>
                <?php endif ?>
            </ul>
        <?php endforeach ?>
    </dl>
</div>

<?php $menu = 'hdgl.cchd'; ?>
<?php $menu=json_encode(explode('.', $menu)); ?>
<script>
    $(function () {
        var cur_menu = '<?php echo ($menu); ?>', cur_menu = eval('[' + cur_menu + ']'), cur_menu = cur_menu[0];
        if (cur_menu == "sy") {
            $("#menu_sy").removeClass('shouye').addClass('current');
        }
        $('#menu_' + cur_menu[0]).click();
        $('#menu_' + cur_menu[0]).find("img").attr("src", "http://img1.isou365.cn/paimai/yunying/images/yun_jie.jpg");
        $(".mianL").css("height", $(document).height() - 95);
        $('#menu_' + cur_menu[1]).addClass('current');
    });
</script>
    <div class="mianR">
        <form action="/SceneActivity/saveAdd" id="activity_form" method="post">
            <div class="live-cont">
                <div class="new-live">
                <ul>
                    <li><span>活动名称</span><input type="text" value="" name="name" id="activity-name" /></li>
                    <li><span>活动类型</span>
                        <select id="type" name="type">
                            <option>请选择活动类型</option>
                            <option value="1">补贴减免</option>
                        </select>
                    </li>
                    <li>
                        <span>所属分公司</span>
                        <select id="activity-company" name="branch_id">
                            <option>请选择分公司</option>
                            <?php if(is_array($bc_list)): foreach($bc_list as $key=>$bc_name): ?><option value="<?php echo ($key); ?>"><?php echo ($bc_name); ?></option><?php endforeach; endif; ?>
                        </select>
                    </li>
                    <li><span>活动规则</span>
                        <select id="activity-rule" name="rules_type">
                            <option value="1">统一减免</option>
                            <option value="2">规则编辑</option>
                        </select>
                    </li>
                    <li><span>活动时间</span>
                        <input type="text" id="demoText9" name="starttime" class="live-time" value="" placeholder="活动开始时间">
                        <i class="time-xian"></i>
                        <input type="text" id="demoText10" name="endtime" class="live-time" value="" placeholder="活动结束时间" readonly="">
                    </li>
                    <li class="new-live-loan">
                        <span class="new-live-money">金额：</span>
                        <input id="activity-money" type="text" name="activity_amount" value="" onkeyup="this.value=this.value.replace(/\D/g,'')"/>
                    </li>
                </ul>
            </div>
                <div class="add-rule clearfix">
                    <div class="rule-text">
                        <h4>活动规则</h4>
                        <ul>
                            <li><span>当竞拍价 ≥</span><input type="text" name="min_bid_price[]" value="" onkeyup="this.value=this.value.replace(/\D/g,'')" /><i>元</i></li>
                            <li><span>当竞拍价 ≤</span><input type="text" name="max_bid_price[]" value="" onkeyup="this.value=this.value.replace(/\D/g,'')" /><i>元</i></li>
                            <li><span>减免</span><input type="text" value="" name="amount[]" onkeyup="this.value=this.value.replace(/\D/g,'')" /><i>元</i></li>
                        </ul>
                    </div>
                <div class="rule-btn"><span></span><p>添加规则</p></div>
            </div>
                <div class="yongBtn">
                <input type="button" value="提交" class="queding" id="determine">
                <input class="quxiao" type="button" onclick="back();" value="取消">
            </div>
            </div>
            <div class="clear"></div>
        </form>

        <div class="rule-text" id="rules" style="display:none">
            <h4>活动规则</h4>
            <ul>
                <li>
                    <span>当竞拍价 ≥</span>
                    <input type="text" name="min_bid_price[]" value="" />
                    <i>元</i>
                </li>
                <li>
                    <span>当竞拍价 ≤</span>
                    <input type="text" name="max_bid_price[]" value="" />
                    <i>元</i>
                </li>
                <li>
                    <span>减免</span>
                    <input type="text" value="" name="amount[]" />
                    <i>元</i>
                </li>
            </ul>
        </div>
    </div>

</div>
<script>

    var arrNumber=[];
    var canSave = false;
    $(function(){
        if($("#activity-rule option:selected").val()=="2"){
            $(".new-live ul li.new-live-loan").hide();
            $(".add-rule").show();
        }
        $("#activity-rule").change(function(){
            if($("#activity-rule option:selected").val()=="2"){
                $(".new-live ul li.new-live-loan").hide();
                $(".add-rule").show();
            }else{
                $(".new-live ul li.new-live-loan").show();
                $(".add-rule").hide();
                $(".add-rule .rule-text").not(":first").remove();
            }
        })


        var rules = [];

//        function initial(){
//            $(".add-rule .rule-text").each(function(){
//                var oInputOne=parseInt($(this).find("input").eq(0).val());
//                var oInputTwo=parseInt($(this).find("input").eq(1).val());
//
//                rules.push([oInputOne,oInputTwo])
//            })
//        }
//
//        initial();

        function validateLastRule(orderV, $inputs){
            var smallValueStr = $inputs.eq(0).val(),
                    largeValueStr = $inputs.eq(1).val(),
                    amountStr = $inputs.eq(1).val(),
                    flag = true;

            // 非空验证
            $inputs.each(function(){
                var that= $(this),
                        curV = that.val();
                if( curV=="" ){
                    that.css("border","solid 1px red");
                    flag = false;
                } else {
                    that.css("border","solid 1px #c5c5c5");
                }
            });
            if ( !flag ) {
                return;
            };

            // 如果最后一条规则和数组中的最后一个元素一致，证明已经通过验证，直接返回(放在非空验证后，是为了方便parseInt)
            var smallNumber=parseInt(smallValueStr),
                    largeNumber=parseInt(largeValueStr);

            if( rules.length > 0 && rules[orderV] && rules[orderV][0] == smallNumber && rules[orderV][1] == largeNumber){
                return true;
            }

            if (smallNumber == largeNumber) {
                shibai_alert('活动规则竞拍价不能相等');
                return false;
            };

            // 大小值的验证
            if (smallNumber > largeNumber) {
                shibai_alert("新添规则大小值不正确！",1000);
                return false;
            };

            // 和已添加的规则验证， 不能产生交集
            for (var i = 0; i < rules.length; i++) {
                if (i == orderV) {continue;}
                var sv = rules[i][0],
                        lv = rules[i][1];
                // 新值是否在已有规则之间
                if (sv <= smallNumber && smallNumber <= lv) {
                    shibai_alert('新添规则和第' + (i+1) + '条规则冲突！',1000);
                    return false;
                };
                if (sv <= largeNumber && largeNumber <= lv) {
                    shibai_alert('新添规则和第' + (i+1) + '条规则冲突！',1000);
                    return false;
                };

                // 已有值是否在新值之间
                if (smallNumber <= sv && sv <= largeNumber) {
                    shibai_alert('新添规则和第' + (i+1) + '条规则冲突！',1000);
                    return false;
                };
                if (smallNumber <= lv && lv <= largeNumber) {
                    shibai_alert('新添规则和第' + (i+1) + '条规则冲突！',1000);
                    return false;
                };

                // if (smallNumber == sv && lv == largeNumber) {
                //     alert("冲突");
                //     return false;
                // };
            };
            if (orderV >= rules.length) {
                rules.push([smallNumber, largeNumber]);
            } else {
                rules[orderV][0] = smallNumber;
                rules[orderV][1] = largeNumber;
            }
            return true;
        }

        function validateRule(orderV, $inputs){
            var smallValueStr = $inputs.eq(0).val(),
                    largeValueStr = $inputs.eq(1).val(),
                    amountStr = $inputs.eq(1).val(),
                    flag = true;

            // 非空验证
            $inputs.each(function(){
                var that= $(this),
                        curV = that.val();
                if( curV=="" ){
                    that.css("border","solid 1px red");
                    flag = false;
                } else {
                    that.css("border","solid 1px #c5c5c5");
                }
            });
            if ( !flag ) {
                return;
            };

            // 如果最后一条规则和数组中的最后一个元素一致，证明已经通过验证，直接返回(放在非空验证后，是为了方便parseInt)
            var smallNumber=parseInt(smallValueStr),
                    largeNumber=parseInt(largeValueStr);

            if (smallNumber==largeNumber) {
                shibai_alert('活动规则竞拍价不能相等');
                return false;
            };

            // 大小值的验证
            if (smallNumber > largeNumber) {
                shibai_alert('第' + rules[orderV] + '条规则，大小值不正确！',1000);

                return false;
            };

            // 和已添加的规则验证， 不能产生交集
            for (var i = 0; i < rules.length; i++) {
                if (i == orderV) {continue;}
                var sv = rules[i][0],
                        lv = rules[i][1];
                // 新值是否在已有规则之间
                if (sv <= smallNumber && smallNumber <= lv) {
                    shibai_alert('第' + (orderV+1) + '条规则和第' + (i+1) + '条规则冲突！',1000);
                    return false;
                };
                if (sv <= largeNumber && largeNumber <= lv) {
                    shibai_alert('第' + (orderV+1) + '条规则和第' + (i+1) + '条规则冲突！',1000);
                    return false;
                };

                // 已有值是否在新值之间
                if (smallNumber <= sv && sv <= largeNumber) {
                    shibai_alert('第' + (orderV+1) + '条规则和第' + (i+1) + '条规则冲突！',1000);
                    return false;
                };
                if (smallNumber <= lv && lv <= largeNumber) {
                    shibai_alert('第' + (orderV+1) + '条规则和第' + (i+1) + '条规则冲突！',100);
                    return false;
                };
            };
            if (orderV >= rules.length) {
                rules.push([smallNumber, largeNumber]);
            } else {
                rules[orderV][0] = smallNumber;
                rules[orderV][1] = largeNumber;
            }
            return true;
        }

        function validateRulesOnSave(){
            var flag = true;
            // if (rules.length == 0) {
            //     alert("规则不能为空！")
            //     return false;
            // };

            $('.add-rule .rule-text').each(function(index, el) {
                var self = $(this),
                    orderV = self.index(),
                    $inputs = self.find(" ul li input"),
                    smallValueStr = $inputs.eq(0).val(),
                    largeValueStr = $inputs.eq(1).val();

                if (rules.length == 0) {
                    flag = validateLastRule(orderV, $inputs);
                    return;
                };

                if (rules[index]) {
                    if(rules[index][0] != smallValueStr || rules[index][1] != largeValueStr){
                        flag = validateRule(orderV, $inputs);
                        if (!flag) {
                            return;
                        };
                    };
                } else {
                    flag = validateLastRule(orderV, $inputs);
                    if (!flag) {
                        return;
                    };
                }

            });
            return flag;
        }


        $(".add-rule .rule-btn").on("click", function(){

            // 验证，添加按钮前的规则（即最后一条规则）
            var self = $(this),
                    orderV = self.prev(".rule-text").index(),
                    $inputs = self.prev(".rule-text").find(" ul li input");
           ///var flag = validateLastRule(orderV, $inputs);
            var flag =  parseInt(orderV) < 0 ? true : validateLastRule(orderV, $inputs);
            if (flag) {
                self.before("<div class='rule-text'><a class='colse-btn'></a>"+$("#rules").html()+"</div>");
            };
        });

        $(".add-rule").delegate(".rule-text .colse-btn","click",function(){
            var self = $(this);
            var indexV=self.parents(".rule-text").index();
            rules.splice(indexV, 1);
            self.parents(".rule-text").remove();
        });




        $(".add-rule ul li input").each(function(){
            $(this).keyup(function(){
                $(this).val($(this).val().replace(/[^0-9.]/g,''));
            })
        });

        $(".queding").click(function(){
            var canSubmit = true;
            if($("#activity-rule option:selected").val() !="1"){ //如果是规则减免的话，需要验证规则
                canSubmit = validateRulesOnSave();
            }
            if (canSubmit) {
                var post_data = "";
                post_data = $("#activity_form").serialize();
                var btn_obj = $(this);
                btn_obj.val("提交中...").attr("disabled",'disabled');
                $.post("/SceneActivity/saveAdd",post_data,function(data){
                    if(data.code!=200){
                        shibai_alert(data.msg,1500);
                        btn_obj.val("确定").removeAttr("disabled style");
                        return false;
                    }
                    btn_alert("添加成功",1500,function(){
                        location.href="/SceneActivity/activity_list";
                    });
                },"json");
            }
        });

    })
    function reduces(obj){
        $(obj).parents(".rule-text").remove();
    }


    function back() {
        window.location.href='/SceneActivity/activity_list';
    }


</script>
<div class="yunBei"></div>
<div class="loading"></div>
</body>
</html>